import * as Comlink from 'comlink'
import WasmWorker from './wasm.worker.js'

const WasmWorkerClass = Comlink.wrap(new WasmWorker())
let wasmWorkerInstance = null
const initWasmWorkerInstance = async () => {
  if (!wasmWorkerInstance) {
    wasmWorkerInstance = await new WasmWorkerClass()
  }
}

// 获取视频的所有帧图片
export const getAllFramePicture = async (file, width = 200, height = 117) => {
  if (!wasmWorkerInstance) {
    await initWasmWorkerInstance()
  }
  return wasmWorkerInstance.getAllFramePicture(file, width, height)
}

// 获取视频文件有多少帧
export const getVideoFrameCount = async (file) => {
  if (!wasmWorkerInstance) {
    await initWasmWorkerInstance()
  }
  return wasmWorkerInstance.getVideoFrameCount(file)
}

// 获取视频第index帧的图片
export const getVideoFramePictureByIndex = async (file, index, width = 200, height = 117) => {
  if (!wasmWorkerInstance) {
    await initWasmWorkerInstance()
  }
  return wasmWorkerInstance.getVideoFramePictureByIndex(file, index, width, height)
}